package com.hang.mapstruct;

import com.hang.entity.Message;
import com.hang.model.vo.MessageInfoVO;
import com.hang.model.vo.MessageVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

import java.util.LinkedList;
import java.util.List;

@Mapper(componentModel = "spring")
public interface MessageMapStruct {
    default List<MessageInfoVO> messageListToMessageInfoVOList(List<Message> messageList){
        List<MessageInfoVO> messageInfoVOList = new LinkedList<>();
        messageList.forEach(
                message -> {
                    messageInfoVOList.add(messageToMessageInfoVO(message));
                }
        );
        return messageInfoVOList;
    }

    @Mapping(source = "messageId",target = "messageId")
    @Mapping(source = "isRead",target = "isRead",qualifiedByName = "readFormatter")
    MessageInfoVO messageToMessageInfoVO(Message message);

    MessageVO messageToMessageVO(Message message);

    @Named("readFormatter")
    default String readFormatter(Integer isRead){
        if(isRead.compareTo(0) == 0){
            return "未读";
        }else if(isRead.compareTo(1) == 0){
            return "已读";
        }
        return "读取状态错误";
    }
}
